from __future__ import print_function
import json
import os
import six
import paddlehub as hub
import xlrd


if __name__ == "__main__":
    # 加载senta模型
    senta = hub.Module(name="senta_bilstm")

    # 把要测试的短文本以str格式放到这个列表里
    file = '匹克态极3.0运动鞋.xlsx'
    wb = xlrd.open_workbook(file)
    sheet1 = wb.sheet_by_index(0)
    comment_content = sheet1.col_values(1)
    comment_content.pop(0)
    # test_text = [
    #     "这家餐厅不是很好吃",
    #     "这部电影差强人意",
    # ]

    # 指定模型输入
    input_dict = {"text": comment_content}

    # 把数据喂给senta模型的文本分类函数
    results = senta.sentiment_classify(data=input_dict)

    # 遍历分析每个短文本
    for index, text in enumerate(comment_content):
        results[index]["text"] = text
    for index, result in enumerate(results):
        if six.PY2:
            print(
                json.dumps(results[index], encoding="utf8", ensure_ascii=False))
        else:
            print('text: {},\t  predict: {}'.format(results[index]['text'],results[index]['sentiment_key']))
